class_name DamageRecorderEffect
extends Effect

# 通用伤害记录效果 - 可以被多个卡牌复用
# 功能：在指定时机记录伤害数值到静态存储

# 配置参数
var record_key: String = ""  # 记录键，用于区分不同卡牌的记录
var turn_used: int = -1      # 记录卡牌使用时的回合数

# 静态变量存储伤害数值（按记录键分组）
static var recorded_damage: Dictionary = {}

func execute(targets: Array[Node]) -> void:
	print("📊 DamageRecorderEffect: 开始记录伤害，记录键=%s" % record_key)
	
	if targets.size() == 0:
		print("❌ 没有目标")
		return
	
	var player = targets[0]
	if not player or not player.stats:
		print("❌ 无效的玩家目标")
		return
	
	# 获取battle引用
	var battle = _get_battle_from_player(player)
	if not battle:
		print("❌ 无法获取battle引用")
		return
	
	# 记录本回合受到的伤害
	var damage_taken = _get_damage_taken_this_turn(player)
	var storage_key = "%s_%d" % [record_key, turn_used]
	recorded_damage[storage_key] = damage_taken
	
	print("📊 DamageRecorderEffect: 记录伤害完成 - 键=%s, 回合=%d, 伤害值=%d" % [record_key, turn_used, damage_taken])
	
	# 播放音效（优先使用基类的sound，其次使用optional_sound）
	# Removed sound and optional_sound from here
	
	print("🔄 DamageRecorderEffect: 伤害记录完成")


# 获取本回合受到的伤害
func _get_damage_taken_this_turn(player: Node) -> int:
	if not player.stats or not player.stats.get_stats_tracker():
		return 0
	
	var stats_tracker = player.stats.get_stats_tracker()
	var combat_tracker = stats_tracker.get_combat_tracker()
	
	if not combat_tracker:
		return 0
	
	# 获取本回合玩家受到的伤害
	var damage_taken = combat_tracker.total_damage_taken
	print("📊 DamageRecorderEffect: 获取伤害 - 本回合受到伤害 %d" % damage_taken)
	
	return damage_taken


# 从玩家获取battle引用
func _get_battle_from_player(player: Node) -> Node:
	if not player.get_tree():
		return null
	
	return player.get_tree().get_first_node_in_group("battle")


# 静态方法：获取记录的伤害值
static func get_recorded_damage(record_key: String, turn: int) -> int:
	var storage_key = "%s_%d" % [record_key, turn]
	return recorded_damage.get(storage_key, 0)


# 静态方法：清理记录的伤害值
static func clear_recorded_damage(record_key: String, turn: int) -> void:
	var storage_key = "%s_%d" % [record_key, turn]
	if recorded_damage.has(storage_key):
		recorded_damage.erase(storage_key)
		print("🗑️ DamageRecorderEffect: 清理伤害记录 - 键=%s" % storage_key)


# 配置方法：设置记录参数
func configure(key: String, turn: int) -> void:
	record_key = key
	turn_used = turn
	print("⚙️ DamageRecorderEffect: 配置完成 - 记录键=%s, 回合=%d" % [record_key, turn_used]) 